package com.aw.baselib.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.TextView;

import com.aw.baselib.R;
import com.aw.baselib.bean.rx.EventReLoginType;
import com.aw.baselib.util.AwActivityUtil;
import com.aw.baselib.util.AwDataUtil;
import com.aw.baselib.util.AwLog;
import com.aw.baselib.util.AwSoftKeyboardUtil;
import com.aw.baselib.util.AwStatusBarUtil;
import com.aw.baselib.util.AwToastUtil;
import com.aw.baselib.widgets.AwViewCustomToolbar;
import com.gyf.barlibrary.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewbinding.ViewBinding;

import static com.gyf.barlibrary.BarHide.FLAG_HIDE_BAR;

/**
 * Created by hzw
 */
@SuppressLint("NewApi")
public abstract class AwBaseBindingActivity<T extends ViewBinding> extends AppCompatActivity implements AwBaseView, View.OnClickListener {
    protected Activity mActivity = AwBaseBindingActivity.this;
    protected T binding = null;
    public String TAG = AwBaseBindingActivity.class.getSimpleName();
    protected AwViewLoadingDialog mLoadingDialog;
    protected AwViewDialog mDialog;
    protected  Bundle mSavedInstanceState = null;
    protected AwViewCustomToolbar mToolbar;
    protected abstract  T getViewBinding();
    protected void initView() {}
    protected void initData() {}
    protected void initListener() {}
    protected void reLogin() {}
    protected void beforeAddView() {}
    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mSavedInstanceState = savedInstanceState;
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
        }
        beforeAddView();
        super.onCreate(savedInstanceState);
        binding = getViewBinding();
        setContentView(binding.getRoot());
        TAG = getClass().getSimpleName();
        mActivity = this;
        //该方法是【友盟+】Push后台进行日活统计及多维度推送的必调用方法，请务必调用！
//        if(PushAgent.getInstance(mActivity) != null) {
//            PushAgent.getInstance(mActivity).onAppStart();
//            MobclickAgent.setCatchUncaughtExceptions(false);//关闭错误统计
//        }
        AwActivityUtil.getInstance().addActivity(mActivity);
        mFragmentManager = getSupportFragmentManager();
        setStatusTransparentDarkTxt();
        initView();
        initData();
        initListener();
        EventBus.getDefault().register(this);
    }

    public FragmentManager getMyFragmentManager() {
        return mFragmentManager;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshByBus(AwBaseEvent type) {
        if(type instanceof EventReLoginType && AwActivityUtil.isTopActivity(mActivity)) {
            reLogin();
        }
    }

    protected void postEvent(AwBaseEvent event) {
        EventBus.getDefault().postSticky(event);
    }

    @Override
    public void showLoadingDialog() {
        if(mActivity == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialog == null) {
                    mLoadingDialog = new AwViewLoadingDialog(mActivity);
                }
                if (!mLoadingDialog.isShowing()) {
                    mLoadingDialog.show();
                }
            }
        });

    }

    @Override
    public void dismissLoadingDialog() {
        if(mActivity == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
                    mLoadingDialog.dismiss();
                }
            }
        });
    }

    public void dismissDialog() {
        if(mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void initDialog() {
        if(mDialog != null) {
            mDialog.dismiss();
        }
        if(mDialog == null) {
            mDialog = new AwViewDialog(mActivity);
        }
    }

    public void showDialogToFinish(String msg) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        initDialog();
        mDialog.showCommonDialog(msg, getString(R.string.common_cancel), getString(R.string.common_close), false, false, null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void showDialogToBack(String msg) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        initDialog();
        mDialog.showCommonDialog(msg, getString(R.string.common_cancel), getString(R.string.common_close), false, false, null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentToBack();
            }
        });
    }

    public void showLoadingDialogWithMsg(final String msg) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialog == null) {
                    mLoadingDialog = new AwViewLoadingDialog(mActivity);
                }
                if (!mLoadingDialog.isShowing()) {
                    mLoadingDialog.show();
                }
                mLoadingDialog.showMsg(msg);
            }
        });
    }

    public void showDialog(String msg) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, getString(R.string.common_cancel), getString(R.string.common_close), false, false, null, null));
    }

    public void showDialog(String msg, View.OnClickListener listener) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, getString(R.string.common_cancel), getString(R.string.common_close), false, false, null, listener));
    }

    public void showDialogWithCancelDismiss(String msg, View.OnClickListener confirmListener) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, getString(R.string.common_cancel), getString(R.string.common_confirm), false, true, null, confirmListener));
    }

    public void showDialogWithCancelFinish(String msg, View.OnClickListener confirmListener) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, getString(R.string.common_cancel), getString(R.string.common_confirm), false, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }, confirmListener));

    }

    public void showDialogCustomLeftAndRight(String msg, String leftTxt, String rightTxt, View.OnClickListener leftListener, View.OnClickListener rightListener) {
        if(mActivity == null) {
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, leftTxt, rightTxt, false, true, leftListener, rightListener));
    }

    public void showInputDialog(String title, String hintMsg, boolean showCancelBtn, AwViewDialog.InputCallback callback) {
        if(mActivity == null) {
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showInputDialog(title, hintMsg, showCancelBtn, callback));
    }

    public void showInputDialogWithOld(String title, String text, AwViewDialog.InputCallback callback) {
        if(mActivity == null) {
            return;
        }
        initDialog();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mDialog.showInputDialogWithOld(title, text, callback);

            }
        });
    }

    public void showCommonCustomDialog(String msg, String leftTxt, String rightTxt, boolean showTopImg, boolean showCancelBtn, final View.OnClickListener leftListener, final View.OnClickListener rightListener) {
        if(mActivity == null) {
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, leftTxt, rightTxt, showTopImg, showCancelBtn, leftListener, rightListener));
    }

    public void showCommonCustomDialog(String msg, String leftTxt, String rightTxt, boolean showTopImg, boolean showCancelBtn, boolean allowCancelOnTouchOutside, final View.OnClickListener leftListener, final View.OnClickListener rightListener) {
        if(mActivity == null) {
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, leftTxt, rightTxt, showTopImg, showCancelBtn, allowCancelOnTouchOutside, leftListener, rightListener));
    }

    public void showCommonCustomDialog(String title, String msg, String leftTxt, String rightTxt, boolean showTopImg, boolean showCancelBtn, boolean allowCancelOnTouchOutside, final View.OnClickListener leftListener, final View.OnClickListener rightListener) {
        if(mActivity == null) {
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(title, msg, leftTxt, rightTxt, showTopImg, showCancelBtn, allowCancelOnTouchOutside, leftListener, rightListener));
    }


    public void showErrorDialog(String msg, View.OnClickListener closeListener) {
        if(mActivity == null) {
            showMsg(msg);
            return;
        }
        initDialog();
        runOnUiThread(() -> mDialog.showCommonDialog(msg, getString(R.string.common_cancel), getString(R.string.common_close), true, false, null, closeListener));
    }

    //========================APP定制部分结束================================


    public void showMsg(final String msg, final String defaultMsg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AwToastUtil.showShort(AwBaseApplication.getInstance(), AwDataUtil.isEmpty(msg) ? defaultMsg : msg);
            }
        });
    }

    @Override
    public void showMsg(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AwToastUtil.showShort(AwBaseApplication.getInstance(), msg);
            }
        });

    }

    public void showMsg(final int msgId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AwToastUtil.showShort(AwBaseApplication.getInstance(), getString(msgId));
            }
        });

    }

    protected void setText(TextView tv, String content) {
        if(null == tv) {
            return;
        }
        if(AwDataUtil.isEmpty(content)) {
            tv.setText("--");
        } else {
            tv.setText(content);
        }
    }

    protected void setTextNoDefault(TextView tv, String content) {
        if(null == tv) {
            return;
        }
        if(AwDataUtil.isEmpty(content)) {
            tv.setText("");
        } else {
            tv.setText(content);
        }
    }

    protected String getText(TextView tv) {
        if(null == tv) {
            return "";
        }
        if(AwDataUtil.isEmpty(tv.getText().toString())) {
            return "";
        }
        return tv.getText().toString().trim();
    }

    @Override
    public void onClick(View v) {

    }

    public void toFinish() {
        //        finish();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition();
        } else {
            finish();
        }
    }

    /**
     * 重写 getResource 方法，防止系统字体影响
     */
    @Override
    public Resources getResources() {//禁止app字体大小跟随系统字体大小调节
        Resources resources = super.getResources();
        if (resources != null && resources.getConfiguration().fontScale != 1.0f) {
            android.content.res.Configuration configuration = resources.getConfiguration();
            configuration.fontScale = 1.0f;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        return resources;
    }

    /**
     * 跳转activity
     *
     * @param clazz
     * @param params 传值格式  key,value
     */
    public void toClassNoAnim(Class clazz, boolean needFinish, Object... params) {
        if (params != null) {
            startActivity(getActivityIntent(clazz, params));
            //            overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
            overridePendingTransition(0, 0);
            if(needFinish) {
                toFinish();
            }
        }
    }

    /**
     * 跳转activity
     *
     * @param clazz
     * @param params 传值格式  key,value
     */
    public void toClass(Class clazz, boolean needFinish, Object... params) {
        if (params != null) {
            startActivity(getActivityIntent(clazz, params));
            //            overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
            //            overridePendingTransition(R.anim.slide_in_from_right, 0);
            //            overridePendingTransition(0, 0);
            if(needFinish) {
                toFinish();
            }
        }
    }

    /**
     * 跳转activity(不传参)
     *
     * @param clazz
     */
    public void toClass(Class clazz, boolean needFinish) {
        startActivity(getActivityIntent(clazz, new Object[0]));
        //        overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
        //        overridePendingTransition(R.anim.slide_in_from_right, 0);
        overridePendingTransition(0, 0);
        if(needFinish) {
            toFinish();
        }
    }

    public void toClassForResult(Class clazz, int requestCode) {
        startActivityForResult(getActivityIntent(clazz, new Object[0]), requestCode);
        //        overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
        //        overridePendingTransition(R.anim.slide_in_from_right, 0);
        //        overridePendingTransition(0, 0);
    }

    public void toClassForResult(Class clazz, int requestCode, Object... params) {
        startActivityForResult(getActivityIntent(clazz, params), requestCode);
        //        overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
        //        overridePendingTransition(R.anim.slide_in_from_right, 0);
        //        overridePendingTransition(0, 0);
    }

    public Intent getActivityIntent(Class var1, Object[] var2) {
        AwLog.d("getActivityIntent mActivity: " + mActivity.getClass().getName());
        AwLog.d("getActivityIntent desActivity: " + var1.getName());
        Intent var3 = new Intent(mActivity, var1);
        if(var2 == null || var2.length == 0) {
            return var3;
        } else {
            int var4 = var2.length;
            if(var4 % 2 != 0) {
                showMsg("参数格式为key,value");
            } else {
                var4 /= 2;

                for(int var5 = 0; var5 < var4; ++var5) {
                    Object var6 = var2[var5 + var5];
                    Object var7 = var2[var5 + var5 + 1];
                    if(!(var6 instanceof String)) {
                        showMsg("参数key类型不对");
                        return var3;
                    }

                    String var8 = (String)var6;
                    if(var7 instanceof String) {
                        var3.putExtra(var8, (String)var7);
                    } else if(var7 instanceof Integer) {
                        var3.putExtra(var8, ((Integer)var7).intValue());
                    } else if(var7 instanceof Boolean) {
                        var3.putExtra(var8, ((Boolean)var7).booleanValue());
                    } else if(var7 instanceof Parcelable) {
                        var3.putExtra(var8, (Parcelable)var7);
                    } else if(var7 instanceof Serializable) {
                        var3.putExtra(var8, (Serializable)var7);
                    }
                }
            }

            return var3;
        }
    }

    /**
     * 跳转fragment
     *
     * @param params 传值格式  key,value
     */
    public void toClass(int viewId, Fragment fragment, boolean addBackStack, Object... params) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        boolean isTagNull = null == mFragmentManager.findFragmentByTag(fragment.getClass().getName());
        AwLog.d("toClass, fragment: " + fragment.getClass().getName() + " ,isAdded: " + fragment.isAdded() + " ,findTag is null?: " + isTagNull);
        if(!fragment.isAdded() && null == mFragmentManager.findFragmentByTag(fragment.getClass().getName())) {
            mFragmentManager.executePendingTransactions();
            if(params != null) {
                try {
                    fragment.setArguments(getFragmentBundle(params));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if(addBackStack) {
                transaction.add(viewId, fragment, fragment.getClass().getName());
                transaction.addToBackStack(fragment.getClass().getName());
            } else {
                transaction.replace(viewId, fragment, fragment.getClass().getName());
            }
            transaction.commitAllowingStateLoss();
        } else {
            AwLog.d("toClassChild, fragment: " + fragment.getClass().getName() + " 已经存在, 直接展示出来");
            transaction.show(fragment).commitAllowingStateLoss();
        }
    }

    /**
     * 跳转fragment(不传参)
     */
    public void toClass(int viewId, Fragment fragment, boolean addBackStack) {
        toClass(viewId, fragment, addBackStack, new Object[0]);
    }

    public Bundle getFragmentBundle(Object[] var2) {
        Bundle bundle = new Bundle();
        if(var2 == null || var2.length == 0) {
            return bundle;
        } else {
            int var4 = var2.length;
            if(var4 % 2 != 0) {
                showMsg("参数格式为key,value");
            } else {
                var4 /= 2;

                for(int var5 = 0; var5 < var4; ++var5) {
                    Object var6 = var2[var5 + var5];
                    Object var7 = var2[var5 + var5 + 1];
                    if(!(var6 instanceof String)) {
                        showMsg("参数key类型不对");
                        return bundle;
                    }

                    String var8 = (String)var6;
                    if(var7 instanceof String) {
                        bundle.putString(var8, (String)var7);
                    } else if(var7 instanceof Integer) {
                        bundle.putInt(var8, ((Integer)var7).intValue());
                    } else if(var7 instanceof Boolean) {
                        bundle.putBoolean(var8, ((Boolean)var7).booleanValue());
                    } else if(var7 instanceof Parcelable) {
                        bundle.putParcelable(var8, (Parcelable)var7);
                    } else if(var7 instanceof Serializable) {
                        bundle.putSerializable(var8, (Serializable)var7);
                    }
                }
            }

            return bundle;
        }
    }

    public boolean isShow(View v) {
        if(v == null)
            return false;
        return v.getVisibility() == View.VISIBLE;
    }

    public void showView(View v, boolean isShow) {
        if (v != null) {
            if (isShow) {
                v.setVisibility(View.VISIBLE);
            } else {
                v.setVisibility(View.GONE);
            }
        }
    }

    public void showViewInvisible(View v, boolean isShow) {
        if (v != null) {
            if (isShow) {
                v.setVisibility(View.VISIBLE);
            } else {
                v.setVisibility(View.INVISIBLE);
            }
        }
    }

    public Fragment getVisibleFragment() {
        @SuppressLint("RestrictedApi") List<Fragment> fragments = mFragmentManager.getFragments();
        if (fragments != null && fragments.size() > 0) {
            for (Fragment fragment : fragments) {
                if (fragment != null && fragment.isVisible()) {
                    return fragment;
                }
            }
        }
        return null;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
        toBack();
    }

    public void toBack() {
        if (hasPopBackStack()) {
            mFragmentManager.popBackStack();
        } else {
            finish();
        }
    }

    public boolean hasPopBackStack() {
        return mFragmentManager.getBackStackEntryCount() > 0;
    }

    public void fragmentToBack() {
        mFragmentManager.popBackStack();
    }

    public void popAllToBack() {
        int count = mFragmentManager.getBackStackEntryCount();
        AwLog.d("popAllToBack count: " + count);
        if(count > 0) {
            for (int i = 0; i < count; ++i) {
                FragmentManager.BackStackEntry backstatck = mFragmentManager.getBackStackEntryAt(i);
                AwLog.d("popAllToBack Fragment", backstatck.getName());
                mFragmentManager.popBackStack();
            }
//            getSupportFragmentManager().popBackStackImmediate(fragment.getClass().getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);

        }
    }

    /**
     * 强制退出程序
     */
    public void ExitAppForced() {
        dismissLoadingDialog();
        //        ActCollector.release();
        //        mApp.clearService();
        System.exit(0);
    }

    public void replaceFragment(int id, Fragment fragment, boolean isAddBack) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        ft.replace(id, fragment);
        if (isAddBack) {
            ft.addToBackStack(null);
        }
        ft.commitAllowingStateLoss();
    }

    public void replaceFragmentArgs(int id, Fragment fragment, Object obj,
                                    boolean isAddBack) {
        Bundle b = new Bundle();
        b.putSerializable("params", (Serializable) obj);
        fragment.setArguments(b);
        replaceFragment(id, fragment, isAddBack);
    }

    protected void setToolbar(String title, AwViewCustomToolbar.OnLeftClickListener leftClickListener) {
        mToolbar = (AwViewCustomToolbar) findViewById(R.id.toolbar_custom);
        if(mToolbar != null) {
            mToolbar.hideRightView();
            mToolbar.setLeftImg(R.drawable.icon_back);
            if(leftClickListener == null) {
                mToolbar.setOnLeftClickListener(() -> {
                    try {
                        AwSoftKeyboardUtil.hideIputKeyboard(mActivity);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    finish();
                });
            } else {
                mToolbar.setOnLeftClickListener(leftClickListener);
            }
            if(!AwDataUtil.isEmpty(title)) {
                mToolbar.setToolbarTitle(title);
            } else {
                mToolbar.hideTitleView();
            }
        }
    }

    protected void setToolbarWithBackImg(String title, AwViewCustomToolbar.OnLeftClickListener leftClickListener) {
        setToolbar(title, leftClickListener);
        if(mToolbar != null) {
            mToolbar.setLeftImg(R.drawable.icon_back);
            mToolbar.hideRightView();
        }
    }

    protected void setToolbarHideLeftAndRight(String title, AwViewCustomToolbar.OnLeftClickListener leftClickListener) {
        setToolbar(title, leftClickListener);
        if(mToolbar != null) {
            mToolbar.hideLeftView();
            mToolbar.hideRightView();
        }
    }
    protected void setToolbarWithBackWithRightImg(String title, int viewId, AwViewCustomToolbar.OnRightClickListener rightClickListener) {
        setToolbar(title, null);
        if(mToolbar != null) {
            mToolbar.setLeftImg(R.drawable.icon_back);
            mToolbar.showRightView();
            mToolbar.setRightImg(viewId);
            mToolbar.setOnRightClickListener(rightClickListener);
        }
    }

    protected void setToolbarHideLeftWithRightImg(String title, int viewId, AwViewCustomToolbar.OnRightClickListener rightClickListener) {
        setToolbar(title, null);
        if(mToolbar != null) {
            mToolbar.hideLeftView();
            mToolbar.showRightView();
            mToolbar.setRightImg(viewId);
            mToolbar.setOnRightClickListener(rightClickListener);
        }
    }

    protected void setToolbarHideLeftWithRightTxt(String title, String txt, AwViewCustomToolbar.OnRightClickListener rightClickListener) {
        setToolbar(title, null);
        if(mToolbar != null) {
            mToolbar.hideLeftView();
            mToolbar.showRightView();
            mToolbar.setRightText(txt);
            mToolbar.setOnRightClickListener(rightClickListener);
        }
    }

    protected void setToolbarHideLeftWithRightTxtImg(String title, String txt, int viewId, AwViewCustomToolbar.OnRightClickListener rightClickListener) {
        setToolbar(title, null);
        if(mToolbar != null) {
            mToolbar.hideLeftView();
            mToolbar.showRightView();
            mToolbar.setRightImgAndText(viewId, txt);
            mToolbar.setOnRightClickListener(rightClickListener);
        }
    }

    protected void setToolbarWithBackCustomImgAndRightTxtImg(String title, int leftViewId, String rightTxt, int rightViewId,
                                                     AwViewCustomToolbar.OnLeftClickListener leftClickListener,
                                                     AwViewCustomToolbar.OnRightClickListener rigthClickListener) {
        mToolbar = (AwViewCustomToolbar) findViewById(R.id.toolbar_custom);
        if(mToolbar != null) {
            if(!AwDataUtil.isEmpty(title)) {
                mToolbar.setToolbarTitle(title);
            } else {
                mToolbar.hideTitleView();
            }
            mToolbar.setLeftImg(leftViewId);
            mToolbar.setRightImgAndText(rightViewId, rightTxt);
            if(null != leftClickListener) {
                mToolbar.setOnLeftClickListener(leftClickListener);
            }
            if(null != rigthClickListener) {
                mToolbar.setOnRightClickListener(rigthClickListener);
            }
        }
    }

    protected void setToolbarWithBackCustomImgAndRightTxt(String title, int leftViewId, String rightTxt,
                                                     AwViewCustomToolbar.OnLeftClickListener leftClickListener,
                                                     AwViewCustomToolbar.OnRightClickListener rigthClickListener) {
        mToolbar = (AwViewCustomToolbar) findViewById(R.id.toolbar_custom);
        if(mToolbar != null) {
            if(!AwDataUtil.isEmpty(title)) {
                mToolbar.setToolbarTitle(title);
            } else {
                mToolbar.hideTitleView();
            }
            mToolbar.setLeftImg(leftViewId);
            mToolbar.setRightText(rightTxt);
            if(null != leftClickListener) {
                mToolbar.setOnLeftClickListener(leftClickListener);
            }
            if(null != rigthClickListener) {
                mToolbar.setOnRightClickListener(rigthClickListener);
            }
        }
    }

    protected void setToolbarWithBackImgAndRightView(String title, String rightTxt, AwViewCustomToolbar.OnRightClickListener rigthClickListener) {
        mToolbar = (AwViewCustomToolbar) findViewById(R.id.toolbar_custom);
        if(mToolbar != null) {
            if(!AwDataUtil.isEmpty(title)) {
                mToolbar.setToolbarTitle(title);
            } else {
                mToolbar.hideTitleView();
            }
            mToolbar.setLeftImg(R.drawable.icon_back);
            mToolbar.setRightText(rightTxt);
            mToolbar.setOnLeftClickListener(new AwViewCustomToolbar.OnLeftClickListener() {
                @Override
                public void onLeftTextClick() {
                    try {
                        AwSoftKeyboardUtil.hideIputKeyboard(mActivity);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    finish();
                }
            });
            if(null != rigthClickListener) {
                mToolbar.setOnRightClickListener(rigthClickListener);
            }
        }
    }

    protected void setToolbarWithBackImgAndRightTextHasColor(String title, String rightTxt, int colorRes, AwViewCustomToolbar.OnRightClickListener rigthClickListener) {
        mToolbar = (AwViewCustomToolbar) findViewById(R.id.toolbar_custom);
        if(mToolbar != null) {
            if(!AwDataUtil.isEmpty(title)) {
                mToolbar.setToolbarTitle(title);
            } else {
                mToolbar.hideTitleView();
            }
            mToolbar.setLeftImg(R.drawable.icon_back);
            mToolbar.setRightTextWithColor(rightTxt, colorRes);
            mToolbar.setOnLeftClickListener(new AwViewCustomToolbar.OnLeftClickListener() {
                @Override
                public void onLeftTextClick() {
                    try {
                        AwSoftKeyboardUtil.hideIputKeyboard(mActivity);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    finish();
                }
            });
            if(null != rigthClickListener) {
                mToolbar.setOnRightClickListener(rigthClickListener);
            }
        }
    }

    protected void setToolbarWithBackImgAndRightView(String title, int rightImg, AwViewCustomToolbar.OnRightClickListener rigthClickListener) {
        mToolbar = (AwViewCustomToolbar) findViewById(R.id.toolbar_custom);
        if(mToolbar != null) {
            if(!AwDataUtil.isEmpty(title)) {
                mToolbar.setToolbarTitle(title);
            } else {
                mToolbar.hideTitleView();
            }
            mToolbar.setLeftImg(R.drawable.icon_back);
            mToolbar.setRightImg(rightImg);
            mToolbar.setOnLeftClickListener(new AwViewCustomToolbar.OnLeftClickListener() {
                @Override
                public void onLeftTextClick() {
                    try {
                        AwSoftKeyboardUtil.hideIputKeyboard(mActivity);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    finish();
                }
            });
            if(null != rigthClickListener) {
                mToolbar.setOnRightClickListener(rigthClickListener);
            }
        }
    }

    protected void setStatusColor(int colorRes) {
        if(mToolbar != null) {
            mToolbar.setBackgroundColor(colorRes);
            AwStatusBarUtil.setColor(mActivity, colorRes, 0);//0 透明 255 黑色
        }
    }

    /**
     * 筛选部分页面, 不允许更改状态栏颜色值
     * @return
     */
    private boolean filterActivity() {
//        if(mActivity instanceof MainActivity
//                || mActivity instanceof NestedScrollingActivity) {
//            return false;
//        }
        return true;
    }

    protected void showToDevMsg() {
        showMsg(getString(R.string.common_process_dev));
    }

    protected void setStatusTransparent() {
        ImmersionBar.with(mActivity).fitsSystemWindows(false)
                .transparentStatusBar()
                .statusBarDarkFont(false)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusTransparentDarkTxt() {
        ImmersionBar.with(mActivity).fitsSystemWindows(false)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusTransparentWithNavigationBar() {
        ImmersionBar.with(mActivity).fitsSystemWindows(false)
                .transparentNavigationBar()
                .statusBarDarkFont(false)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusHideNavigationBar() {
        ImmersionBar.with(mActivity).fitsSystemWindows(false)
                .hideBar(FLAG_HIDE_BAR)
                .statusBarDarkFont(false)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusTxtDark() {
        ImmersionBar.with(mActivity).fitsSystemWindows(true)
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(R.color.white)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusToSpecialColor(int color) {
        ImmersionBar.with(mActivity).fitsSystemWindows(true)
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(color)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusBlue() {
        ImmersionBar.with(mActivity).fitsSystemWindows(true)
                .statusBarDarkFont(false)
                .statusBarColor(R.color.colorPrimary)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusWhite() {
        ImmersionBar.with(mActivity).fitsSystemWindows(true)
                .statusBarDarkFont(false)
                .statusBarColor(R.color.white)
                .keyboardEnable(true)
                .init();
    }

    protected void setStatusBlack() {
        ImmersionBar.with(mActivity).fitsSystemWindows(true)
                .statusBarDarkFont(false)
                .statusBarColor(R.color.black, 0.5f)
                .keyboardEnable(true)
                .init();
    }

    /** 针对8.0设备, 透明Activity会导致Only fullscreen opaque activities can request orientation, 参考https://blog.csdn.net/starry_eve/article/details/82777160解决如下 */
    private boolean fixOrientation(){
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo)field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private boolean isTranslucentOrFloating(){
        boolean isTranslucentOrFloating = false;
        try {
            int [] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean)m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    /** 针对8.0设备, 透明Activity会导致Only fullscreen opaque activities can request orientation, 参考https://blog.csdn.net/starry_eve/article/details/82777160解决如上 */
    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AwActivityUtil.getInstance().removeActivity(mActivity);
        EventBus.getDefault().unregister(this);
        ImmersionBar.with(mActivity).destroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
